package com.argrace.smart.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.alink.business.alink.ALinkBusinessEx;
import com.aliyun.alink.sdk.net.anet.api.AError;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryRequest;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryResponse;
import com.zhouyou.view.seekbar.SignSeekBar;
import com.argrace.smart.R;
import com.argrace.smart.base.BaseActivity;
import com.argrace.smart.base.ConstantConfigure;
import com.argrace.smart.entity.Key_ValueEntity;
import com.argrace.smart.entity.MineZigBeeDeviceListEntity;
import com.argrace.smart.glide.GliderImageLoaderUtil;
import com.argrace.smart.model.LocalDb_QingJing_DeviceTable;
import com.argrace.smart.ui.DrawableSwitch;
import com.argrace.smart.util.AlinkHttpUtil;
import com.argrace.smart.util.utils;
import java.util.HashMap;

/**
 * Created by argrace_dev_1 on 2018/2/13.
 */

public class DeviceControl_MenSuoActivity extends BaseActivity {
    private TextView bufang, baojing, zhuangtai, kaimen;
    MineZigBeeDeviceListEntity bean;
    private ImageView img;
    private SignSeekBar dianliang, xinhaoqiangdu;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_devicecontrol_mensuo);
        init();
    }

    private void init() {
        bean = (MineZigBeeDeviceListEntity) getIntent().getSerializableExtra("deviceinfo");
        setView();
        showRight(true, R.mipmap.icon_modify);
        //进入设备修改界面
        setLeftandRightClickListeners(null, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DeviceControl_MenSuoActivity.this, DeviceModifyActivity.class);
                intent.putExtra("uuid", bean.getUuid());
                intent.putExtra("model", bean.getModel());
                startActivity(intent);
            }
        });
        getDeviceDetailInfo();
    }

    private void setView() {
        img = (ImageView) findViewById(R.id.activity_devicecontrol_img);
        String imgurl = null;
        dianliang = (SignSeekBar) findViewById(R.id.activity_devicecontrol_dianliang);
        xinhaoqiangdu = (SignSeekBar) findViewById(R.id.activity_devicecontrol_xinhaoqiangdu);

        bufang = (TextView) findViewById(R.id.activity_devicecontrol_mensuo_bufang);
        baojing = (TextView) findViewById(R.id.activity_devicecontrol_mensuo_baojing);
        zhuangtai = (TextView) findViewById(R.id.activity_devicecontrol_mensuo_zhuangtai);
        kaimen = (TextView) findViewById(R.id.activity_devicecontrol_mensuo_kaimen);
        if (bean.getModel().equals(ConstantConfigure.MODEL_ZHINENGMENSUO)) {
            imgurl = ConstantConfigure.IMGURL_ZHINENGMENSUO_00141E48;
        }
        if (imgurl != null) {
            GliderImageLoaderUtil.load(this, imgurl, img);
        }

    }

    //查询设备最新状态
    private void getDeviceDetailInfo() {
        String method = "mtop.openalink.app.core.device.get.status";
        HashMap<String, Object> params = new HashMap<>();
        params.put("uuid", bean.getUuid());
        AlinkHttpUtil.AlinkHttpRequest(method, params, new ALinkBusinessEx.IListener() {
            @Override
            public void onSuccess(TransitoryRequest transitoryRequest, TransitoryResponse transitoryResponse) {
                if (transitoryResponse == null || transitoryResponse.data == null) {
                    return;
                }
                Log.i("info", "子设备最新状态成功" + transitoryResponse.data);
                JSONObject obj = JSON.parseObject(transitoryResponse.data.toString());
                fillData(obj, obj.getString("uuid"));
            }

            @Override
            public void onFailed(TransitoryRequest transitoryRequest, AError aError) {
                Log.i("info", "子设备最新状态失败" + aError.getMsg() + aError.getCode());
            }
        });
    }

    private void fillData(JSONObject obj, String uuid) {
        if (uuid == null || !uuid.equals(bean.getUuid())) {
            return;
        }
        if (bean.getModel().equals(ConstantConfigure.MODEL_ZHINENGMENSUO)) {
            Key_ValueEntity bean_dianliang = JSON.parseObject(obj.getString("BatteryPercentage"), Key_ValueEntity.class);
            Key_ValueEntity bean_xinhao = JSON.parseObject(obj.getString("Rssi"), Key_ValueEntity.class);
            Key_ValueEntity bean_bufang = JSON.parseObject(obj.getString("ArmMode"), Key_ValueEntity.class);
            Key_ValueEntity bean_baojing = JSON.parseObject(obj.getString("LockAlarm"), Key_ValueEntity.class);
            Key_ValueEntity bean_zhuangtai = JSON.parseObject(obj.getString("LockState"), Key_ValueEntity.class);
            Key_ValueEntity bean_kaimen = JSON.parseObject(obj.getString("OpenDoor"), Key_ValueEntity.class);
            dianliang.setProgress(Long.parseLong(bean_dianliang.getValue()));
            xinhaoqiangdu.setProgress(Long.parseLong(bean_xinhao.getValue()));
            if (bean_bufang != null && bean_bufang.getValue() != null) {
                //0（所有防区撤防） 1（在家布防） 2（睡眠布防） 3（所有防区布防）
                switch (bean_bufang.getValue()) {
                    case "0":
                        bufang.setText("所有防区撤防");
                        break;
                    case "1":
                        bufang.setText("在家布防");
                        break;
                    case "2":
                        bufang.setText("睡眠布防");
                        break;
                    case "3":
                        bufang.setText("所有防区布防");
                        break;
                }
            }
            if (bean_baojing != null && bean_baojing.getValue() != null) {
                //0（正常） 1（门栓阻塞报警） 2（恢复出厂设置报警） 5（尝试开锁次数过多，门锁冻结报警） 6（门锁被撬报警） 10（用户输入挟持密码报警）
                switch (bean_baojing.getValue()) {
                    case "0":
                        baojing.setText("正常");
                        break;
                    case "1":
                        baojing.setText("门栓阻塞报警");
                        break;
                    case "2":
                        baojing.setText("恢复出厂设置报警");
                        break;
                    case "5":
                        baojing.setText("尝试开锁次数过多，门锁冻结报警");
                        break;
                    case "6":
                        baojing.setText("门锁被撬报警");
                        break;
                    case "10":
                        baojing.setText("用户输入挟持密码报警");
                        break;
                }
            }
            if (bean_zhuangtai != null && bean_zhuangtai.getValue() != null) {
                //0（正常锁门） 1（门外反锁） 2（门没上锁） 3（门内反锁）
                switch (bean_zhuangtai.getValue()) {
                    case "0":
                        zhuangtai.setText("正常锁门");
                        break;
                    case "1":
                        zhuangtai.setText("门外反锁");
                        break;
                    case "2":
                        zhuangtai.setText("门没上锁");
                        break;
                    case "3":
                        zhuangtai.setText("门内反锁");
                        break;
                }
            }
            if (bean_kaimen != null && bean_kaimen.getValue() != null) {
                //0（正常锁门） 1（门外反锁） 2（门没上锁） 3（门内反锁）
                String user = JSONObject.parseObject(bean_kaimen.getValue()).getString("UserId");
                if (user != null) {
                    kaimen.setText(user);
                }
            }
        }

    }

    @Override
    protected void dorecieve(JSONObject obj) {
        super.dorecieve(obj);
        if (obj == null || obj.getJSONObject("params") == null || obj.getJSONObject("params").getJSONObject("data") == null || obj.getJSONObject("params").getString("uuid") == null) {
            return;
        }
        fillData(obj.getJSONObject("params").getJSONObject("data"), obj.getJSONObject("params").getString("uuid"));
    }
}
